package com.sky.demo.thread;

import com.sky.demo.redis.RedisUtil;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class RunnableDemo {

    private static final int MAX_POOL_SIZE = 80;
    private static final String QUEUE_KEY = "QUEUE_KEY";
    private static final ThreadPoolExecutor executor = new ThreadPoolExecutor(10, MAX_POOL_SIZE, 60, TimeUnit.SECONDS, new LinkedBlockingDeque<>(MAX_POOL_SIZE));
    private RedisUtil redisUtil;


    @Before
    public void init(){
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        redisUtil = new RedisUtil(jedis);
    }

    public void sync(){

        for(int i = 0; i < 10; i++){
            redisUtil.lpush(i + "", i + "");
        }


        for(int i = 0; i < 10; i++){
            int finalI = i;
            executor.submit(new Runnable() {
                @Override
                public void run() {
                    redisUtil.rpop(finalI + "");
                    System.out.println("test3: " + Thread.currentThread().getName());
                    ((Runnable) () -> {
                        System.out.println("test4: " + Thread.currentThread().getName());
                    }).run();
                }
            });
        }
    }

    @Test
    public void test(){
        sync();
    }


    public static void main(String[] args){
        System.out.println("test: " + Thread.currentThread().getName());


        for(int i = 0; i < 10; i++){
            new Thread(() -> {
                System.out.println("test2: " + Thread.currentThread().getName());

                ((Runnable) ()-> {
                    System.out.println("test3: " + Thread.currentThread().getName());
                }).run();
            }).start();
        }
    }
}
